package example;

import java.net.*;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.io.*;
import java.util.*;

import message.UniversalMessage;
import message.UniversalMessage.MSG_TYPE;


public class WorkerThread extends Thread {
    
    public  DBConnection dbConn = null;
    public boolean isFinish = false;
    private Socket socket = null;
    public SimpleProtocol kpp;
    
    public String[] cmdBuffer;
    public int transactionID;
    int iSocket = 0;
    
    //Query processing
    ArrayList<Tuples> localResult = new ArrayList<Tuples>();
    
    //this thread is data thread
    //run at data server
    //get request from master server
    //execute query
    //return result to master result
    //and done
    public WorkerThread(Socket socket, SimpleProtocol kpp, int queryId) throws RemoteException, Exception {
		super("WorkerThread");
		this.socket = socket;
		this.kpp = kpp;
		dbConn = new DBConnection();
//		dbConn.updateCurrentTransactionNumber(this.tableName);
    }
   
    //input: tableName
    //output: Host IP
    @SuppressWarnings("deprecation")
	public void run(){
		UniversalMessage msg = new UniversalMessage();
		boolean isFinish = false;
		try {
			ObjectInputStream in = new ObjectInputStream(this.socket.getInputStream());
			ObjectOutputStream out = new ObjectOutputStream(this.socket.getOutputStream());			
			while (!isFinish && (msg = (UniversalMessage)in.readObject()) != null) {
				//from client
				switch(msg.getMsgType()) {
				//client query
				case QUERY:
					//just execute query and return to server
					processQuery(socket, msg);
					break;
				//data server send hash join data to another data server
				case DATA:
					processQueryOnMemmory(socket, msg);
					break;
				}
				//from data server for hash join query?
			}	
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
    }

	private void processQueryOnMemmory(Socket socket2, UniversalMessage msg) {
		// TODO Auto-generated method stub
		//do hashjoin on memory
		//return result to master server
		//possibly tough
		
	}

	//process Range and local join query
	private void processQuery(Socket socket2, UniversalMessage msg) {
		// TODO Auto-generated method stub		
		switch (msg.getQueryType()) {
		case RANGE:
			executeRangeQuery(msg.getQuery());
			break;
		case JOIN:
			executeJoinQuery(msg.getQuery());
			break;
		}
		
	}

	private void executeJoinQuery(String query) {
		// TODO Auto-generated method stub
		MiniTask mini;
		try {
			mini = new MiniTask(query);
			mini.run();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void executeRangeQuery(String query) {
		// TODO Auto-generated method stub
		MiniTask mini;
		try {
			mini = new MiniTask(query);
			mini.run();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}

}
